<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/wang-cheng/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wang-cheng/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wang-cheng/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wang-cheng/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wang-cheng/css/main.css">


<link rel="stylesheet" href="/wang-cheng/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"cheng-wang123.gitee.io","root":"/wang-cheng/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="Spring IOC传统的程序开发过程中，如果要使用一个对象，就必须new一个出来，用完后再将这个对象销毁。 所谓的控制反转(IOC)，就是将我们new一个对象的工作，交给框架去完成，对于Spring框架来说，就是将对象的创建、销毁的控制权，都交给Spring来控制，也就是说控制这个对象的生命周期的不再是引用它的对象，而是变成了Spring。 所谓的依赖注入(DI)，是指动态的向某个对象提高它所需">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring学习笔记(一)">
<meta property="og:url" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/index.html">
<meta property="og:site_name" content="诚の博客">
<meta property="og:description" content="Spring IOC传统的程序开发过程中，如果要使用一个对象，就必须new一个出来，用完后再将这个对象销毁。 所谓的控制反转(IOC)，就是将我们new一个对象的工作，交给框架去完成，对于Spring框架来说，就是将对象的创建、销毁的控制权，都交给Spring来控制，也就是说控制这个对象的生命周期的不再是引用它的对象，而是变成了Spring。 所谓的依赖注入(DI)，是指动态的向某个对象提高它所需">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/IOC%E4%B8%A4%E7%A7%8D%E5%AE%B9%E5%99%A8.jpg">
<meta property="article:published_time" content="2022-03-06T08:09:02.000Z">
<meta property="article:modified_time" content="2022-03-19T05:58:00.754Z">
<meta property="article:author" content="王诚">
<meta property="article:tag" content="Spring理论知识">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/IOC%E4%B8%A4%E7%A7%8D%E5%AE%B9%E5%99%A8.jpg">

<link rel="canonical" href="https://cheng-wang123.gitee.io/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Spring学习笔记(一) | 诚の博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/wang-cheng/atom.xml" title="诚の博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wang-cheng/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">诚の博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录学习中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/wang-cheng/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/wang-cheng/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/wang-cheng/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/wang-cheng/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://cheng-wang123.gitee.io/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wang-cheng/images/avatar.gif">
      <meta itemprop="name" content="王诚">
      <meta itemprop="description" content="命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="诚の博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring学习笔记(一)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-03-06 16:09:02" itemprop="dateCreated datePublished" datetime="2022-03-06T16:09:02+08:00">2022-03-06</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-03-19 13:58:00" itemprop="dateModified" datetime="2022-03-19T13:58:00+08:00">2022-03-19</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Spring-IOC"><a href="#Spring-IOC" class="headerlink" title="Spring IOC"></a><strong>Spring IOC</strong></h1><p>传统的程序开发过程中，如果要使用一个对象，就必须new一个出来，用完后再将这个对象销毁。</p>
<p>所谓的控制反转(IOC)，就是将我们new一个对象的工作，交给框架去完成，对于<code>Spring</code>框架来说，就是将对象的创建、销毁的控制权，都交给<code>Spring</code>来控制，也就是说控制这个对象的生命周期的不再是引用它的对象，而是变成了<code>Spring</code>。</p>
<p>所谓的依赖注入(DI)，是指动态的向某个对象提高它所需要的其他对象。比如当我们需要一个对象<code>A</code>去操作数据库，以前我们会在代码中new 一个<code>Connection</code>对象指向<code>A</code>对象。对于<code>Spring</code>框架来说，我们只需要告诉<code>Spring</code>，<code>A</code>需要一个<code>Connection</code>对象，<code>Spring</code>就会在适当的时候为我们创建一个<code>Connection</code>对象指向<code>A</code>。<code>A</code>需要依赖<code>Connection</code>对象才能运行，<code>Spring</code>将<code>Connection</code>注入到<code>A</code>,就是<strong>依赖注入</strong></p>
<h2 id="IOC容器"><a href="#IOC容器" class="headerlink" title="IOC容器"></a><strong>IOC容器</strong></h2><p><code>Spring</code>中提供了两种IOC容器，用于存放交由<code>Spring</code>控制的对象(bean)：</p>
<p><strong>BeanFactory</strong></p>
<p><strong>ApplicationContext</strong></p>
<img src="/wang-cheng/2022/03/06/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%B8%80/IOC%E4%B8%A4%E7%A7%8D%E5%AE%B9%E5%99%A8.jpg" class title="IOC两种容器">

<p>可以看到，ApplicationContext是BeanFactory的子类。</p>
<h3 id="BeanFactory"><a href="#BeanFactory" class="headerlink" title="BeanFactory"></a><strong>BeanFactory</strong></h3><p>基础类型IOC容器。默认采用延迟初始化策略(lazy-load)，只有当客户端对象需要访问容器中某个被管理的对象时，才对该被管理对象进行初始化以及依赖注入操作。对于资源有限，并且功能要求不是很严格的场景，<code>BeanFactory</code>是比较合适的<code>IOC</code>容器选择</p>
<h3 id="ApplicationContext"><a href="#ApplicationContext" class="headerlink" title="ApplicationContext"></a><strong>ApplicationContext</strong></h3><p><code>ApplicationContext</code>是<code>BeanFactory</code>的子类，除了拥有<code>BeanFactory</code>的所有功能之外，还提供了比如事件发布、国际化信息支持等。<code>ApplicationContext</code>所管理的对象，在该容器启动之后，默认全部初始化并完成依赖注入。在那些资源充足，并且要求更多功能的场景中，<code>ApplicationContext</code>是比较合适的容器选择。</p>
<h2 id="初探IOC容器是如何工作的"><a href="#初探IOC容器是如何工作的" class="headerlink" title="初探IOC容器是如何工作的"></a><strong>初探IOC容器是如何工作的</strong></h2><p><strong>以注解方式为例子</strong></p>
<ol>
<li><p>根据配置类指定的扫描路径，扫描路径下所有class文件对象，将扫描到的class文件对象封装成类元数据信息。类元数据信息包括类的名字、父类及父类名字、实现的接口名字、内部类名字，判断是否是接口、是否是抽象类、是否是一个注解，获取类上添加的注解信息、获取方法上的注解信息、获取集合上的注解信息。</p>
</li>
<li><p>对类元数据信息进行判断，判断这些类元数据信息能不能成为一个bean。判断方式包括判断该类有没有在excludeFilters配置的排除范围内，是否有标志为bean的注解(@Component、@Bean、@Service等)等。</p>
</li>
<li><p>判断都满足后会生成对应的<code>BeanDifinition</code>对象，该对象记录了该类的的信息，包括类的名称、单例还是原型等信息。</p>
</li>
<li><p><code>BeanDifinition</code>对象就绪后，还会生成beanName，然后会去容器中判断是否存在相同的beanName，存在的话就抛出beanName已存在异常，不存在则将<code>BeanDifinition</code>注册到容器的<code>BeanDifinitionMap</code>中。</p>
</li>
<li><p>向<code>BeanDifinitionMap</code>注册完成后，会再从中取出<code>BeanDifinition</code>去实例化对象</p>
</li>
<li><p>实例化对象的第一步是先加载类，通过读取<code>BeanDifinition</code>中所对应的class，根据class进行加载。</p>
</li>
<li><p>在类加载成功后，便可以进行实例化了。但在实例化前和实例化后，可以通过实现<code>BeanPostProcessor</code>接口，在实例化前和后进行一些业务操作。</p>
</li>
<li><p>实例化。通过类去实例化一个对象。</p>
</li>
<li><p>完成了对象实例化以及实例化前后的操作，开始对属性进行依赖注入。依赖注入的逻辑比较多，简单来说就是通过判断Bean的属性中是否有对应的@Autowired、@Resource、@Value等注解,或利用构造函数，从实例化的对象的集合中取出对象进行注入。</p>
</li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/wang-cheng/tags/Spring%E7%90%86%E8%AE%BA%E7%9F%A5%E8%AF%86/" rel="tag"># Spring理论知识</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/03/04/%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86/" rel="prev" title="动态代理">
      <i class="fa fa-chevron-left"></i> 动态代理
    </a></div>
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/03/07/Spring%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E4%BA%8C/" rel="next" title="Spring学习笔记(二)">
      Spring学习笔记(二) <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Spring-IOC"><span class="nav-number">1.</span> <span class="nav-text">Spring IOC</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#IOC%E5%AE%B9%E5%99%A8"><span class="nav-number">1.1.</span> <span class="nav-text">IOC容器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#BeanFactory"><span class="nav-number">1.1.1.</span> <span class="nav-text">BeanFactory</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ApplicationContext"><span class="nav-number">1.1.2.</span> <span class="nav-text">ApplicationContext</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9D%E6%8E%A2IOC%E5%AE%B9%E5%99%A8%E6%98%AF%E5%A6%82%E4%BD%95%E5%B7%A5%E4%BD%9C%E7%9A%84"><span class="nav-number">1.2.</span> <span class="nav-text">初探IOC容器是如何工作的</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">王诚</p>
  <div class="site-description" itemprop="description">命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wang-cheng/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/wang-cheng/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王诚</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/wang-cheng/lib/anime.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.ui.min.js"></script>

<script src="/wang-cheng/js/utils.js"></script>

<script src="/wang-cheng/js/motion.js"></script>


<script src="/wang-cheng/js/schemes/pisces.js"></script>


<script src="/wang-cheng/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
